package com.zcbl.toolkit.logger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.embedded.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

/**
 * @author he peng
 * @create 2018/5/7 14:50
 * @see
 */

@Configuration
@ConditionalOnClass(EnableAsync.class)
@EnableAsync
public class SurveyBizLogAutoConfiguration {

    @Qualifier("singleThreadExecutor")
    @Bean
    public Executor singleThreadExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(1);
        executor.setMaxPoolSize(1);
        executor.setQueueCapacity(300);
        executor.setThreadNamePrefix("SurveyBizLogExecutor-");
        executor.initialize();
        return executor;
    }


    @Bean
    public FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean filterRegistration = new FilterRegistrationBean();
        filterRegistration.setFilter(new HttpRequestSurveyBizLogFilter());
        filterRegistration.setOrder(Integer.MIN_VALUE);
        filterRegistration.setAsyncSupported(true);
        filterRegistration.setName("httpRequestSurveyBizLogFilter");
        filterRegistration.addUrlPatterns("/*");
        return filterRegistration;
    }

    @Autowired
    @Bean
    public SurveyBizDBLogger surveyBizDBLogger(SurveyCaseOperateDAO surveyCaseOperateDAO) {
        return new SurveyBizDBLogger(surveyCaseOperateDAO);
    }

}
